package appconfig

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"moments/global"
	"moments/handle/model"
)

var (
	err error
)

func InitMysql() {
	mysqlData := global.ConfigData.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", mysqlData.User, mysqlData.Password, mysqlData.Host, mysqlData.Port, mysqlData.Database)
	global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("mysql连接失败")
		return
	} else {
		fmt.Println("mysql连接成功")
	}
	err = global.DB.AutoMigrate(&model.User{}, &model.Friend{}, &model.FriendType{}, &model.Blacklist{})
	if err != nil {
		fmt.Println("迁移失败")
		return
	} else {
		fmt.Println("迁移成功")
	}

}
